<template>
  <div class="container">
    <div class="user_list">
      <div class="table-page-search-wrapper">
        <a-form layout="inline" @keyup.enter.native="search">
          <a-form-item label="日期选择">
            <a-range-picker
              v-model="queryParam.time"
              :ranges="{
              '1周': [moment().startOf('day').subtract(1, 'weeks'), moment()],
              半个月: [moment().startOf('day').subtract(14, 'days'), moment()],
              '3个月': [moment().startOf('day').subtract(3, 'months'), moment()],
              '6个月': [moment().startOf('day').subtract(6, 'months'), moment()],
              '1年': [moment().startOf('day').subtract(1, 'years'), moment()],
            }"
              format="YYYY-MM-DD"
              @change="onChangeData"
              :disabled-date="disabled"
            />
          </a-form-item>
          <a-form-item label="用户ID">
            <a-input v-model="queryParam.userNo" placeholder="用户ID" />
          </a-form-item>
          <a-form-item label="登录名">
            <a-input v-model="queryParam.userPhone" placeholder="登录名" />
          </a-form-item>
          <a-form-item label="身份证">
            <a-input v-model="queryParam.idNumber" placeholder="身份证" />
          </a-form-item>
          <a-form-item label="审核状态">
            <a-select v-model="queryParam.review" placeholder="请选择审核状态" style="width: 200px">
              <a-select-option :key="item.key" v-for="item in option" @onchange="onchange(item)">
                {{ item.label }}
              </a-select-option>
            </a-select>
          </a-form-item>
          <a-form-item>
            <a-button type="primary" htmlType="submit" @click="search">查询</a-button>
            <a-button htmlType="submit" class="ml15" @click="searchReset">重置</a-button>
          </a-form-item>
        </a-form>
      </div>
      <s-table
        ref="table"
        size="default"
        :columns="columns"
        :data="loadData"
        bordered
        :rowKey="(record) => record.id"
      >
        <div slot="paymentType" slot-scope="text">
          <span v-if="text === 1">银行卡</span>
          <span v-else>其他</span>
        </div>
        <!-- <div slot="walletBalance" slot-scope="text">
          <span>{{ numFormat(text) }}</span>
        </div>
        <div slot="withdrawAmount" slot-scope="text">
          <span>{{ numFormat(text) }}</span>
        </div> -->
        <div slot="review" slot-scope="text">
          <span v-if="+text === 1">待审核</span>
          <span v-else-if="+text === 2">审核成功</span>
          <span v-else-if="+text === 3">审核驳回</span>
          <span v-else>-</span>
        </div>
        <span slot="action" slot-scope="text, record">
        <a @click="view(record)">{{ +record.review === 1 ? '审核' : '查看' }}</a>
      </span>
      </s-table>
      <a-modal v-model="visible" :title="title" class="modal">
        <template #footer>
          <a-button type="primary" v-if="title === '查看'" @click="visible = false"> 关闭 </a-button>
          <a-button type="primary" v-if="title !== '查看'" @click="visible1 = true"> 通过 </a-button>
          <a-button type="danger" v-if="title !== '查看'" @click="visible2 = true"> 驳回 </a-button>
        </template>
        <div class="user_harder_img">
          <span>头像：</span>
          <img :src="require('@/assets/images/header.png')" alt="" />
        </div>
        <div class="user_info">
          <div class="item">
            <div>
              <span>用户ID：</span><span>{{ info?.userNo }}</span>
            </div>
            <div>
              <span>登录名：</span><span>{{ info?.userPhone }}</span>
            </div>
          </div>
          <div class="item">
            <div>
              <span>登录名：</span><span>{{ info?.userPhone }}</span>
            </div>
            <div>
              <span>注册时间：</span><span>{{ info?.registrationTime }}</span>
            </div>
          </div>
          <div class="item">
            <div>
              <span>用户状态：</span><span>{{ +info?.userState === '1' ? '禁用' : '启用' }}</span>
            </div>
            <div>
              <span>当前余额：</span><span>{{ common.numFormat(info?.walletBalance) }}</span>
            </div>
          </div>
        </div>
        <div class="user_info" v-if="title === '查看'">
          <div class="item">
            <div>
              <span>收款方式：</span><span>{{ info?.paymentType === 1 ? '银行卡' : '其他' }}</span>
            </div>
            <div>
              <span>提现金额：</span><span>{{ common.numFormat(info?.withdrawAmount) }}</span>
            </div>
          </div>
          <div class="item">
            <div>
              <span>银行卡号：</span><span>{{ info?.bankNumber }}</span>
            </div>
            <div>
              <span>姓名：</span><span>{{ info?.userName }}</span>
            </div>
          </div>
          <div class="item">
            <div>
              <span>银行名称：</span><span>{{ info?.bankName }}</span>
            </div>
            <div>
              <span>银行编码：</span><span>{{ info?.bankId }}</span>
            </div>
          </div>
        </div>
        <div class="user_info" v-else>
          <div class="item">
            <div>
              <span>提现金额：</span><span>{{ common.numFormat(info?.withdrawAmount) }}</span>
            </div>
            <div>
              <span>收款方式：</span><span>{{ info?.paymentType === 1 ? '银行卡' : '其他' }}</span>
            </div>
          </div>
          <div class="item">
            <div>
              <span>银行名称：</span><span>{{ info?.bankName }}</span>
            </div>
            <div>
              <span>银行卡号：</span><span>{{ info?.bankNumber }}</span>
            </div>
          </div>
        </div>
        <div class="user_info" v-if="title === '查看'">
          <div class="item">
            <div>
              <span>审核人ID：</span><span>{{ info?.reviewUserId }}</span>
            </div>
            <div>
              <span>审核人昵称：</span><span>{{ info?.reviewUserNickName }}</span>
            </div>
          </div>
          <div class="item">
            <div>
              <span>审核时间：</span><span>{{ info?.reviewTime }}</span>
            </div>
            <div>
              <span>审核结果：</span><span>{{ reviewType[+info?.review] }}</span>
            </div>
          </div>
          <div class="item">
            <span>驳回原因：</span>
            <a-input v-model="info.remark" type="textarea" disabled :rows="4" class="textarea" />
          </div>
        </div>
      </a-modal>
      <a-modal title="提示信息" v-model="visible1" @ok="handleOk(1)">
        <p>确定通过该提现？</p>
        <p>注：[用户ID：{{ info.id }}], 减少" {{ info.withdrawAmount }} "</p>
      </a-modal>
      <a-modal title="驳回" v-model="visible2" @ok="handleOk(2)">
        <div class="item">
          <span>驳回原因：</span>
          <a-input v-model="rationale" type="textarea" :rows="4" class="textarea" />
        </div>
      </a-modal>
    </div>
  </div>
</template>

<script>
import STable from '@/components/Table'
import { userWithdrawalList, userWithdrawInfo, audits } from '@/api/user-manage'
import { mapGetters } from 'vuex'
import moment from 'moment'
import { common } from '@/common/index'
export default {
  name: 'userList',
  components: {
    STable,
  },
  computed: {
    ...mapGetters(['userInfo']),
  },
  data() {
    return {
      form: this.$form.createForm(this),
      visible: false,
      visible1: false,
      visible2: false,
      title: '',
      info: {},
      rationale: '', // 驳回原因：
      option: [
        { label: '待审核', value: '1', key: '1' },
        { label: '审核通过', value: '2', key: '2' },
        { label: '审核驳回', value: '3', key: '3' },
      ],
      columns: [
        {
          title: '创建时间',
          dataIndex: 'createTime',
          align: 'center'
        },
        {
          title: '用户ID',
          dataIndex: 'userNo',
          align: 'center'
        },
        {
          title: '登录名',
          dataIndex: 'userPhone',
          align: 'center'
        },
        {
          title: '身份证',
          dataIndex: 'idNumber',
          align: 'center'
        },
        {
          title: '当前余额',
          dataIndex: 'walletBalance',
          customRender: (text) => this.common.numFormat(text),
          align: 'center'
        },
        {
          title: '提现金额',
          dataIndex: 'withdrawAmount',
          customRender: (text) => this.common.numFormat(text),
          align: 'center'
        },
        {
          title: '收款方式',
          dataIndex: 'paymentType',
          slots: { title: 'paymentType' },
          scopedSlots: { customRender: 'paymentType' },
          align: 'center'
        },
        {
          title: '审核状态',
          dataIndex: 'review',
          slots: { title: 'review' },
          scopedSlots: { customRender: 'review' },
          align: 'center'
        },
        {
          title: '操作',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' },
          align: 'center'
        },
      ],
      // 查询条件参数
      queryParam: { time: [null, null], queryUserId: '', userPhone: '', review: '' },
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        const { userId } = this.userInfo

        this.queryParam.review = this.queryParam.review !== '' ? +this.queryParam.review : ''
        let params = {
          ...parameter,
          userId,
          ...this.queryParam,
          startTime: this.queryParam.time[0] ? `${this.queryParam.time[0]} 00:00:00` : '',
          endTime: this.queryParam.time[1] ? `${this.queryParam.time[1]} 23:59:59` : '',
        }
        delete params.time
        return new Promise((resolve, reject) => {
          userWithdrawalList(params)
            .then((res) => {
              if (res.code === 200) {
                resolve(res.data)
              } else {
                this.$message.error(res?.msg)
              }
            })
            .catch((err) => {
              reject(err)
            })
            .finally(() => {})
        })
      },
      reviewType: ['', '等待审核', '审核通过', '审核拒绝', '提现成功', '提现失败'],
    }
  },
  methods: {
    moment,
    onChangeData(dates, dateStrings) {
      this.queryParam.time = dateStrings
    },
    disabled(current) {
      return current && current > moment().subtract().endOf('day')
    },
    view(row) {
      this.visible = true
      this.title = +row.review === 1 ? '审核' : '查看'
      const { userId } = this.userInfo
      let params = {
        id: row.id,
        userId,
      }
      userWithdrawInfo(params).then((res) => {
        if (res.code === 200) {
          this.info = res.data ?? {}
        }
      })
    },
    refresh(isBackToFirstPage = false) {
      this.$refs.table.refresh(isBackToFirstPage)
    },
    onchange(data) {
      this.queryParam.review = data.key
    },
    search() {
      this.refresh(false)
    },
    searchReset() {
      this.queryParam = { time: [null, null], queryUserId: '', userPhone: '', review: '' }
      this.refresh(true)
    },
    handleOk(type) {
      const { userId } = this.userInfo
      const params = {
        id: this.info.id,
        orderNumber: this.info.orderNumber,
        remark: type === 2 ? this.rationale : '',
        review: type,
        userId,
      }
      audits(params).then((res) => {
        if (res.code === 200) {
          this.$message.success(res.msg)
          this.visible1 = false
          this.visible = false
          this.visible2 = false
          this.$refs.table.refresh()
        } else {
          this.$message.error(res.msg)
        }
      })
    },
  },
}
</script>
<style lang="less" scoped>
.user_list {
  .form {
    display: flex;
    margin-bottom: 20px;
    flex-wrap: wrap;
  }
}
.modal {
  /deep/ .ant-modal {
    width: 1000px !important;
  }
  .user_harder_img {
    display: flex;
    align-items: center;
    margin-bottom: 20px;
    span {
      padding-left: 15px;
    }
    img {
      width: 30px;
      height: 30px;
      border-radius: 50%;
    }
  }
  .user_info {
    display: flex;
    flex-direction: column;
    margin-bottom: 20px;
    border-bottom: 1px solid #f3f4f6;
    &:last-child {
      border-bottom: none;
    }
    .item {
      display: flex;
      div {
        height: 40px;
        width: 50%;
      }
      span {
        &:first-child {
          width: 100px;
          text-align: left;
          padding: 0 15px;
        }
        &:last-child {
          text-align: left;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
        }
      }
      .textarea {
        width: 80%;
      }
    }
  }
}
</style>
